package org.as3search.criteria {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import org.as3search.config.SearchConfiguration;
	
	/**
	 * <code>RegExpExpression</code> evaluates that a property's
	 * value matches a regular expression.
	 */
	public class RegExpExpression extends SimpleExpression {
		/**
		 * Creates a new <code>RegExpExpression</code> instance.
		 * @param	propertyName	The name of the property to match to the regular expression.
		 * @param	value			The value, a String or RegExp, to match to the value of 
		 * 		<code>propertyName</code>.
		 * @param	configuration	The <code>SearchConfiguration</code> instance to assist in comparisions.
		 */
		public function RegExpExpression(propertyName:String, value:Object,
				configuration:SearchConfiguration = null, ignoreCase:Boolean=false) {
			super(propertyName, value, configuration, ignoreCase);
		}
		/**
		 * @inheritDoc
		 */
		override public function evaluateProperty(propertyValue:Object):Boolean {
			if (propertyValue == null || value == null) 
				return false;
			
			var regex:RegExp = value is String ? new RegExp(value, ignoreCase ? "i" : "") : RegExp(value);
			return regex.test(propertyValue.toString());
		}
	}
}